package com.dropdb.demo.config;

import com.dropdb.demo.bean.enumeration.ErrorReason;
import com.dropdb.demo.bean.vo.BaseRespVO;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(AuthenticationException.class)
    public BaseRespVO unauthenticatedHandler(AuthenticationException auth) {
        auth.printStackTrace();
        return BaseRespVO.error(ErrorReason.AUTH_FAILED.getCode(), ErrorReason.AUTH_FAILED.getReason());
    }

    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public Object unauthorizedHandler(AuthorizationException e) {
        e.printStackTrace();
        return BaseRespVO.error(ErrorReason.NO_PERMISSION.getCode(), ErrorReason.NO_PERMISSION.getReason());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public BaseRespVO globalExceptionHandler(Exception e) {
        e.printStackTrace();
        return BaseRespVO.error(ErrorReason.INTERNAL_ERROR.getCode(), ErrorReason.INTERNAL_ERROR.getReason());
    }

}
